<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
			<el-form-item label="档案区编号" prop="areaNo">
				<el-tree-select  @change="changeArea" v-model="queryParams.areaNo" :data="areaOptions"
					:props="{ value: 'archAreaNo', label: 'label', children: 'children' }" value-key="archAreaNo"
					placeholder="请选择档案区" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案柜" prop="cabinetNo">
				<el-tree-select v-model="queryParams.cabinetNo" :data="cabinetOptions"
					:props="{ value: 'colNo', label: 'label', children: 'children' }" value-key="colNo"
					placeholder="请选择档案柜" clearable @keyup.enter="handleQuery" @click="getListArchiveCabinet" />
			</el-form-item>
			<el-form-item label="档案盒条码" prop="boxBarCode">
				<el-input v-model="queryParams.boxBarCode" placeholder="请输入档案盒条码" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="盒号" prop="boxNo">
				<el-input v-model="queryParams.boxNo" placeholder="请输入盒号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="库位号" prop="houseLocationNo">
				<el-input v-model="queryParams.houseLocationNo" placeholder="请输入库位号" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案盒状态" prop="boxStatus">
				<el-select v-model="queryParams.boxStatus" placeholder="请选择档案盒状态">
					<el-option v-for="dict in da_box_status" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="记录时间" style="width: 308px;">
				<el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
					start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="推送状态" prop="pushStatus">
				<el-select v-model="queryParams.pushStatus" placeholder="请选择推送状态" clearable>
					<el-option v-for="dict in push_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport"
					v-hasPermi="['archive:ArchiveBoxStatus:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="ArchiveBoxStatusList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="序号" align="center" prop="id" width="55" />
			<el-table-column label="库区编号" align="center" prop="areaNo" width="120" />
			<el-table-column label="柜组编号" align="center" prop="cabinetNo" width="120" />
			<el-table-column label="档案盒条码" align="center" prop="boxBarCode" width="190" />
			<el-table-column label="盒号" align="center" prop="boxNo" width="150" />
			<el-table-column label="库位号" align="center" prop="houseLocationNo" width="200" />
			<!--      <el-table-column label="档案盒状态(1.档案入库、2.档案借阅、3.借阅归还、4.档案销毁、5.档案转出、6.档案转入、7.档案盒移位)" align="center" prop="boxStatus" />-->
			<el-table-column label="档案盒状态" align="center" prop="boxStatus" width="130">
				<template #default="scope">
					<dict-tag :options="da_box_status " :value="scope.row.boxStatus" />
				</template>
			</el-table-column>
			<el-table-column label="记录时间" align="center" prop="recordTime" width="170">
				<template #default="scope">
					<span>{{ parseTime(scope.row.recordTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="推送状态" align="center" prop="pushStatus" width="100">
				<template #default="scope">
					<dict-tag :options="push_status" :value="scope.row.pushStatus" />
				</template>
			</el-table-column>
			<!-- <el-table-column label="推送时间" align="center" prop="pushTime" width="170">
				<template #default="scope">
					<span>{{ parseTime(scope.row.pushTime) }}</span>
				</template>
			</el-table-column> -->
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
				<!--        <template #default="scope">-->
				<!--          <el-button-->
				<!--            type="text"-->
				<!--            icon="Edit"-->
				<!--            @click="handleUpdate(scope.row)"-->
				<!--            v-hasPermi="['archive:ArchiveBoxStatus:edit']"-->
				<!--          >修改</el-button>-->
				<!--          <el-button-->
				<!--            type="text"-->
				<!--            icon="Delete"-->
				<!--            @click="handleDelete(scope.row)"-->
				<!--            v-hasPermi="['archive:ArchiveBoxStatus:remove']"-->
				<!--          >删除</el-button>-->
				<!--        </template>-->
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleView(scope.row, scope.index)"
						v-hasPermi="['archive:ArchiveBoxStatus:query']">详细</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />


		<!-- 添加或修改盒状态记录对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body>
			<el-form ref="ArchiveBoxStatusRef" :model="form" :rules="rules" label-width="110px">
				<el-form-item label="库区编号" prop="areaNo">
					<el-input v-model="form.areaNo" placeholder="请输入库区编号" />
				</el-form-item>
				<el-form-item label="柜组编号" prop="cabinetNo">
					<el-input v-model="form.cabinetNo" placeholder="请输入柜组编号" />
				</el-form-item>
				<el-form-item label="档案盒条码" prop="boxBarCode">
					<el-input v-model="form.boxBarCode" placeholder="请输入档案盒条码" />
				</el-form-item>
				<el-form-item label="盒号" prop="boxNo">
					<el-input v-model="form.boxNo" placeholder="请输入盒号" />
				</el-form-item>
				<el-form-item label="库位号" prop="houseLocationNo">
					<el-input v-model="form.houseLocationNo" placeholder="请输入库位号" />
				</el-form-item>
				<el-form-item label="记录时间" prop="recordTime">
					<!--          <el-date-picker clearable-->
					<!--            v-model="form.recordTime"-->
					<!--            type="datetime"-->
					<!--            value-format="yyyy-mm-dd hh:mm:ss"-->
					<!--            placeholder="请选择记录时间">-->
					<!--          </el-date-picker>-->
					<el-input v-model="form.recordTime" placeholder="请输入记录时间" />
				</el-form-item>
				<el-form-item label="推送时间" prop="pushTime">
					<!--          <el-date-picker clearable-->
					<!--            v-model="form.pushTime"-->
					<!--            type="datetime"-->
					<!--            value-format="yyyy-mm-dd hh:mm:ss"-->
					<!--            placeholder="请选择推送时间">-->
					<!--          </el-date-picker>-->
					<el-input v-model="form.pushTime" placeholder="请输入推送时间" />
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>

		<!-- 详细 -->
		<el-dialog title="档案盒状态记录详细" v-model="openscan" width="800px" append-to-body>
			<el-form ref="ArchiveBoxStatusRef" :model="form" label-width="130px">
				<el-row>
					<el-col :span="12">
						<el-form-item label="库区编号：">
							<el-input v-model="form.areaNo" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="柜组编号：">
							<el-input v-model="form.cabinetNo" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="档案盒条码：">
							<el-input v-model="form.boxBarCode" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="档案盒号：">
							<el-input v-model="form.boxNo" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="库位号：">
							<el-input v-model="form.houseLocationNo" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="档案盒状态：">
							<template #default="scope">
								<dict-tag :options="da_box_status" :value="form.boxStatus" />
							</template>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="推送标识：">
							<template #default="scope">
								<dict-tag :options="push_status" :value="form.pushStatus" />
							</template>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="openscan = false">关 闭</el-button>
				</div>
			</template>
		</el-dialog>

	</div>
</template>

<script setup name="ArchiveBoxStatus">
	import {
		listArchiveBoxStatus,
		getArchiveBoxStatus,
		delArchiveBoxStatus,
		addArchiveBoxStatus,
		updateArchiveBoxStatus
	} from "@/api/archive/ArchiveBoxStatus";
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	import {
		listArchiveCabinetAll
	} from "@/api/archive/ArchiveCabinet";
	import {
		listArchiveArchAreaAll
	} from "@/api/archive/ArchiveArchArea";
	const {
		proxy
	} = getCurrentInstance();

	const ArchiveBoxStatusList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const cabinetOptions = ref([]);
	const areaOptions = ref([]);
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			areaNo: null,
			cabinetNo: null,
			boxBarCode: null,
			boxNo: null,
			houseLocationNo: null,
			boxStatus: null,
			recordTime: null,
			pushStatus: null,
			pushTime: null
		},
		rules: {}
	});
	const dateRange = ref([]);
	const {
		queryParams,
		form,
		rules
	} = toRefs(data);
	const {
		push_status,
		da_box_status
	} = proxy.useDict('push_status', 'da_box_status');
	const openscan = ref(false);
	/** 查询盒状态记录列表 */
	function getList() {
		loading.value = true;
		//listArchiveBoxStatus(queryParams.value).then(response => {
		listArchiveBoxStatus(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
			ArchiveBoxStatusList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			id: null,
			areaNo: null,
			cabinetNo: null,
			boxBarCode: null,
			boxNo: null,
			houseLocationNo: null,
			boxStatus: "0",
			recordTime: null,
			pushStatus: "0",
			pushTime: null
		};
		proxy.resetForm("ArchiveBoxStatusRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		dateRange.value = [];
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.id);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加盒状态记录";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const id = row.id || ids.value
		getArchiveBoxStatus(id).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改盒状态记录";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["ArchiveBoxStatusRef"].validate(valid => {
			if (valid) {
				if (form.value.id != null) {
					updateArchiveBoxStatus(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addArchiveBoxStatus(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const id = row.id || ids.value;
		proxy.$modal.confirm('是否确认删除盒状态记录编号为"' + id + '"的数据项？').then(function() {
			return delArchiveBoxStatus(ids);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/ArchiveBoxStatus/export', {
			...queryParams.value
		}, `档案盒状态记录_${new Date().getTime()}.xlsx`)
	}

	function handleView(row) {
		openscan.value = true;
		form.value = row;
	}
	//根据档案区查询档案柜
	function changeArea() {
		cabinetOptions.value = [];
		form.value.cabinetNo = null;
		queryParams.value.cabinetNo = null;
	}
	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchAreaAll().then(response => {
			areaOptions.value = response.rows;
		});
	};

	/** 查询档案柜下拉树结构 */
	function getListArchiveCabinet(queryq) {
		const query = {
			archiveAreaNo: form.value.areaNo || queryParams.value.areaNo
		}
		cabinetOptions.value = [];
		// 检查查询条件是否为空
		if (!query || !query.archiveAreaNo) {
			proxy.$modal.msgError("请选择档案区编号！");
			return;
		}

		listArchiveCabinetAll(query).then(response => {
			cabinetOptions.value = response.rows;
		});
	};

	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchAreaAll().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};

	initAreaOptions();
	getList();
</script>
